import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Map<String, String[]> paramMap = req.getParameterMap();
		String param = paramMap.get("url")[0];
		String[] params = param.split("url=");
		if(params!=null && params.length>1){
			params[1] = URLEncoder.encode(params[1]);
		}
		StringBuffer sb = new StringBuffer();
		for(int i=0,length=params.length;i<length;i++){
			sb.append(params[i]);
			if(i==0){
				sb.append("url=");
			}
		}
		String result = HttpClientUtil.doPost(paramMap, sb.toString());
		resp.setHeader("Content-Type", "text/json;charset=utf-8");
		System.out.println(">>>>"+result);
		resp.getWriter().write(result);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Map<String, String[]> paramMap = req.getParameterMap();
		String result = HttpClientUtil.doGet(paramMap, paramMap.get("url")[0]);
		resp.setHeader("Content-Type", "text/json;charset=utf-8");
		System.out.println(">>>>"+result);
		resp.getWriter().write(result);
	}
	
	@Override
	protected void doHead(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doHead(req, resp);
	}
}
